package com.cucn.ingmall.cart.controller;

import com.cucn.common.utils.R;
import com.cucn.ingmall.cart.interceptor.CartInterceptor;
import com.cucn.ingmall.cart.service.CartService;
import com.cucn.ingmall.cart.vo.Cart;
import com.cucn.ingmall.cart.vo.CartItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author KouChaoJie
 * @since: 2022/1/14 10:30
 */
@Controller
@Slf4j
public class CartController {
    @Resource
    private CartService cartService;

    /**
     * 跳转购物车列表页
     *
     * @return 购物车列表页
     */
    @GetMapping("/cart.html")
    public String cartList(Model model) throws ExecutionException, InterruptedException {
        //1.快速得到当前用户信息id,userKeyValue
        log.info("当前用户信息:{}", CartInterceptor.threadlocal.get());

        Cart cart = cartService.getCart();
        model.addAttribute("cart", cart);

        return "cartList";
    }

    /**
     * 添加商品到购物车
     *
     * @param skuNum 商品数量
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer skuNum,
                            RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        log.info("用户添加商品:{},数量:{},到购物车", skuId, skuNum);
        CartItem cartItem = cartService.addToCart(skuId, skuNum);
        redirectAttributes.addAttribute("skuId", skuId);

        return "redirect:http://cart.koukoucoding.xn--fiqs8s/addToCartSuccess.html";
    }

    /**
     * 添加商品转成功页
     *
     * @param skuId
     * @return
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId, Model model) {
        CartItem cartItem = cartService.getSuccessCart(skuId);

        model.addAttribute("cartItem", cartItem);

        return "success";
    }

    /**
     * 购物项的选中状态
     *
     * @param skuId   商品id
     * @param checked 是否选中 1,0
     */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId, @RequestParam("checked") Integer checked) {
        log.info("更改购物项选中状态");
        cartService.updateCartItemCheck(skuId, checked);
        log.info("更改购物项选中状态完成:{}", checked);
        return "redirect:http://cart.koukoucoding.xn--fiqs8s/cart.html";
    }

    /**
     * 改变购物车中商品数量
     *
     * @param skuId 商品id
     * @param num   商品数量
     */
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num) {
        log.info("更改购物项数量");
        cartService.updateCartItemCount(skuId, num);
        log.info("更改购物项:{}数量:{},完成", skuId, num);
        return "redirect:http://cart.koukoucoding.xn--fiqs8s/cart.html";
    }

    /**
     * 删除购物项
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        log.info("删除购物项");
        cartService.deleteCartItem(skuId);
        log.info("删除购物项:{},完成", skuId);
        return "redirect:http://cart.koukoucoding.xn--fiqs8s/cart.html";
    }

    /**
     * 获取当前用户选中的购物项
     */
    @ResponseBody
    @GetMapping("/currentUserCartItems")
    public R currentUserCartItems() {
        List<CartItem> checkCartItems = cartService.getCheckItems();
        return R.ok().setData(checkCartItems);
    }
}
